#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
    int nums[1010];
public:
    int numSquares(int n) {
        int m = 0;
        for (int i = 1;i * i <= n;i++)
        {
            nums[i] = i * i;
            m++;
        }
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0x3f3f3f3f));
        dp[0][0] = 0;
        for (int i = 1;i <= m;i++)
        {
            for (int j = 0;j <= n;j++)
            {
                dp[i][j] = dp[i - 1][j];
                if (j >= nums[i])
                    dp[i][j] = min(dp[i][j], dp[i][j - nums[i]] + 1);
            }
        }
        return dp[m][n];
    }
};